cmake_minimum_required(VERSION 3.12)
project(darknetPlay)

# 设置Darknet版本
set(Darknet_MAJOR_VERSION 0)
set(Darknet_MINOR_VERSION 2)
set(Darknet_PATCH_VERSION 5)
set(Darknet_TWEAK_VERSION 4)
set(Darknet_VERSION ${Darknet_MAJOR_VERSION}.${Darknet_MINOR_VERSION}.${Darknet_PATCH_VERSION}.${Darknet_TWEAK_VERSION})
message("Darknet版本: ${Darknet_VERSION}")

# 设置选项
set(ENABLE_BUILD_TENSORT OFF CACHE BOOL "Enable build  Tensort ")
message("Enable build  Tensort : ${ENABLE_BUILD_TENSORT}")

# 设置编译选项
option(CUDA_VERBOSE_BUILD "创建详细的CUDA编译" ON)
option(BUILD_SHARED_LIBS "作为共享库构建darknet" ON)
option(ENABLE_OPENCV "启用OpenCV集成" ON)
option(ENABLE_CUDA "启用CUDA支持" OFF)
option(ENABLE_CUDNN "启用CUDNN" ON)
option(VCPKG_BUILD_OPENCV_WITH_CUDA "使用CUDA扩展构建OpenCV" ON)
option(VCPKG_USE_OPENCV4 "使用OpenCV 4" ON)

# 设置预处理定义
if(WIN32)
  add_definitions(
    NDEBUG
    _CONSOLE
    _CRT_SECURE_NO_WARNINGS
    CUDNN_HALF
    CUDNN
    GPU
    _TIMESPEC_DEFINED
    _SCL_SECURE_NO_WARNINGS
    WIN32
    WIN64
    USE_WEBSOCKET
  )
   message("WIN32")
elseif(UNIX AND NOT APPLE)
  add_definitions(
    -DLINUX
    -DUSE_WEBSOCKET
    -DWEBRTC_LINUX
    -DWEBRTC_POSIX  
    -DNO_SSL 
    -DNO_OPENSSL  
  )
  message("UNIX AND NOT APPLE")
endif()

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++17 -fPIC")
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-fPIC")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)


# 设置默认构建类型
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "未指定构建类型，设置为 '${default_build_type}'。")
  set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
      STRING "选择构建类型." FORCE)
  # 设置CMake-GUI中可选的构建类型
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
    "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

# 设置安装路径
set(INSTALL_BIN_DIR      "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "exe和dll的安装路径")
set(INSTALL_LIB_DIR      "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "lib的安装路径")
set(INSTALL_INCLUDE_DIR  "include/darknet"           CACHE PATH "头文件的安装路径")
set(INSTALL_CMAKE_DIR    "share/darknet"             CACHE PATH "cmake配置的安装路径")


# 添加Darknet库的路径
set(DARKNET_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rd/darknet)
include_directories(${DARKNET_ROOT_DIR}/include)
if(ENABLE_CUDA)
link_directories(${DARKNET_ROOT_DIR}/lib/linuxgpu)
else()
link_directories(${DARKNET_ROOT_DIR}/lib/linux86)
endif()



# 添加webrtc库的路径
set(WEBRTC_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rd/webrtc)

set(LIBFFMPEG_INCLUDE_PATH   ./3rd/libffmpeg/include)
set(LIBFFMPEG_LIB_PATH   ${CMAKE_CURRENT_SOURCE_DIR}/../../mcu/code/libFFmpeg/lib)

# 设置包含目录
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${LIBFFMPEG_INCLUDE_PATH}  
  ./inc
  ${WEBRTC_ROOT_PATH}/include
  ${WEBRTC_ROOT_PATH}/include/third_party/abseil-cpp
  ${WEBRTC_ROOT_PATH}/include/third_party/libyuv/include
  ${WEBRTC_ROOT_PATH}/include/third_party/boringssl/src/include
  ${SOC_INCLUDE_DIRS}
  ./civetweb/include
  ./videoQualityDetection
  ${CMAKE_CURRENT_SOURCE_DIR}/common
  ${CMAKE_CURRENT_SOURCE_DIR}/3rd/spdlog/include
)

if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
      link_directories(
 			${LIBFFMPEG_LIB_PATH}
			${WEBRTC_ROOT_PATH}/lib/arm64
			)
else()
       link_directories(
			${LIBFFMPEG_LIB_PATH}
			${WEBRTC_ROOT_PATH}/lib/linux86
			)
endif()


# 如果启用了OpenCV，查找OpenCV包
if(ENABLE_OPENCV)
	set(OpenCV_INCLUDE_DIRS  "/usr/include/opencv4/")
	set(OpenCV_LIB_DIRS  "/usr/lib/aarch64-linux-gnu")
	set(OPENCV_LIBS opencv_imgproc opencv_core opencv_highgui opencv_imgcodecs opencv_videoio)
	include_directories(${OpenCV_INCLUDE_DIRS})
	link_directories(${OpenCV_LIB_DIRS})
	add_definitions(-DOPENCV)
	message("start opencv : ${OpenCV_INCLUDE_DIRS}")
endif()



if(ENABLE_BUILD_TENSORT)

	add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/TensorRT TensorRT_build)
	
	   # 动态设置输出目录，包含平台和构建类型
    set(output_directory "${CMAKE_BINARY_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}")

    # 设置 TensorRTools 库的生成路径
    set_target_properties(TensorRTools PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY "${output_directory}"       # 静态库输出路径
        LIBRARY_OUTPUT_DIRECTORY "${output_directory}"       # 动态库输出路径
        RUNTIME_OUTPUT_DIRECTORY "${output_directory}"       # 可执行文件输出路径
    )

    # 获取并打印 TensorRTools 库的 LIBRARY_OUTPUT_DIRECTORY
    get_target_property(lib_output_dir TensorRTools LIBRARY_OUTPUT_DIRECTORY)
    message("TensorRT_build LIBRARY_OUTPUT_DIRECTORY: ${lib_output_dir}")
	link_directories(${LIBRARY_OUTPUT_DIRECTORY})
endif()





# 添加源文件
file(GLOB_RECURSE SOURCE_FILES
  ${CMAKE_CURRENT_SOURCE_DIR}/capture/*.cpp
  ${CMAKE_CURRENT_SOURCE_DIR}/civetweb/src/*.*  
  ${CMAKE_CURRENT_SOURCE_DIR}/common/*.cpp
  ${CMAKE_CURRENT_SOURCE_DIR}/videoQualityDetection/*.cpp
  ${CMAKE_CURRENT_SOURCE_DIR}/src/*.*
 
)

# 生成可执行程序
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE inc) 



# 动态设置输出目录，包含平台和构建类型
set(output_directory "${CMAKE_BINARY_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}/${CMAKE_BUILD_TYPE}")

# 设置 TensorRTools 库的生成路径
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY "${output_directory}"       # 静态库输出路径
        LIBRARY_OUTPUT_DIRECTORY "${output_directory}"       # 动态库输出路径
        RUNTIME_OUTPUT_DIRECTORY "${output_directory}"       # 可执行文件输出路径
    )		
			
# 获取并打印可执行程序的 RUNTIME_OUTPUT_DIRECTORY
get_target_property(runtime_output_dir ${CMAKE_PROJECT_NAME} RUNTIME_OUTPUT_DIRECTORY)
message("Executable RUNTIME_OUTPUT_DIRECTORY: ${runtime_output_dir}")


# 配置 -fpermissive 编译选项，只对该目标生效
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -fpermissive)

if(ENABLE_OPENCV)
	# 链接库
	target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS})
	target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${OpenCV_LIBS_PATH})
	target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ${OpenCV_LIBS}) # 手动指定要链接的 OpenCV 库
endif()

target_link_libraries(${CMAKE_PROJECT_NAME}  PRIVATE		
			webrtc
			-lpthread 
			FFmpeg-linux
			-ldl
			-latomic	
			${OPENCV_LIBS}			
)
if(ENABLE_BUILD_TENSORT)
target_link_libraries(${CMAKE_PROJECT_NAME}  PRIVATE
			TensorRTools 		
)
endif()